package happy.video

import android.content.Context
import android.media.MediaPlayer
import android.net.Uri
import android.util.Log
import android.view.Surface
import com.ss.ugc.android.alpha_player.model.VideoInfo
import com.ss.ugc.android.alpha_player.player.AbsPlayer
import tv.danmaku.ijk.media.exo.IjkExoMediaPlayer
import tv.danmaku.ijk.media.player.AndroidMediaPlayer
import tv.danmaku.ijk.media.player.IMediaPlayer
import tv.danmaku.ijk.media.player.IjkMediaPlayer

/**
 * @description:
 * @author:  wj
 * @date :   2025/6/16 21:02
 */
class IjkGiftPlayer(val context: Context) : AbsPlayer(context) {
    private lateinit var ijkMediaPlayer: IjkExoMediaPlayer
    private var mSurface: Surface? = null


    override fun getPlayerType(): String {
        return "IjkGiftPlayer"
    }

    override fun getVideoInfo(): VideoInfo {
        Log.e(getPlayerType(), "getVideoInfo: "+ijkMediaPlayer.videoHeight)
        return VideoInfo(
            ijkMediaPlayer.videoWidth, ijkMediaPlayer.videoHeight
        )
    }

    override fun initMediaPlayer() {
        Log.e(getPlayerType(), "initMediaPlayer: ")
        ijkMediaPlayer = IjkExoMediaPlayer(context)
        ijkMediaPlayer.setOnPreparedListener {
            Log.e(getPlayerType(), "setOnPreparedListener: ")
            preparedListener?.onPrepared() // 真正的播放开始

        }
        ijkMediaPlayer.setOnCompletionListener {
            completionListener?.onCompletion()
        }

        ijkMediaPlayer.setOnErrorListener { mp, what, extra ->
            errorListener?.onError(what, extra, "")
            false
        }
        ijkMediaPlayer.setOnInfoListener { mp, what, extra ->
            if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
                firstFrameListener?.onFirstFrame() // 关键点第一帧画面到达
            }else if (what ==702) {
                firstFrameListener?.onFirstFrame() // 关键点第一帧画面到达
            }
            false
        }
        Log.e(getPlayerType(), "initMediaPlayer: done ")

    }


    override fun pause() {
        ijkMediaPlayer.pause()
    }

    override fun prepareAsync() {
        Log.e(getPlayerType(), "prepareAsync: " + mSurface)
        ijkMediaPlayer.setOnPreparedListener {
            Log.e(getPlayerType(), "setOnPreparedListener: ")
            preparedListener?.onPrepared() // 真正的播放开始

        }
        ijkMediaPlayer.setOnCompletionListener {
            completionListener?.onCompletion()
        }

        ijkMediaPlayer.setOnErrorListener { mp, what, extra ->
            errorListener?.onError(what, extra, "")
            false
        }
        ijkMediaPlayer.setOnInfoListener { mp, what, extra ->
            Log.e(getPlayerType(), "prepareAsync: setOnInfoListener what"+what )
            if (what == MediaPlayer.MEDIA_INFO_VIDEO_RENDERING_START) {
                firstFrameListener?.onFirstFrame() // 关键点第一帧画面到达
            }else if (what ==702) {
                firstFrameListener?.onFirstFrame() // 关键点第一帧画面到达
            }
            false
        }
        mSurface?.run { // ijkExoplayer
            ijkMediaPlayer.setSurface(this)
        }
        ijkMediaPlayer.prepareAsync()
//        ijkMediaPlayer.start()
    }

    override fun release() {
        ijkMediaPlayer.release()
    }

    override fun reset() {
//        ijkMediaPlayer.
     ijkMediaPlayer.reset()
    }

    override fun setDataSource(dataPath: String) {
        Log.e(getPlayerType(), "setDataSource: " + dataPath)
        ijkMediaPlayer.setDataSource(dataPath)
//        reset()
    }

    override fun setLooping(looping: Boolean) {
//        ijkMediaPlayer.isLooping=(looping)
    }

    override fun setScreenOnWhilePlaying(onWhilePlaying: Boolean) {
        ijkMediaPlayer.setScreenOnWhilePlaying(onWhilePlaying)
    }

    override fun setSurface(surface: Surface) {
        Log.e(getPlayerType(), "setSurface: " + ijkMediaPlayer)
        mSurface = surface
        ijkMediaPlayer.setSurface(surface)

    }

    override fun start() {
        Log.e(getPlayerType(), "start: " + ijkMediaPlayer)
        ijkMediaPlayer.start()
    }

    override fun stop() {
        ijkMediaPlayer.stop()
    }
}